/*jslint sloppy: true */
/*global require */
var angular = require('angular');
var LM = require('../global');
require('./list.html');

LM.controller('CaseList', function ($scope, Case, $routeParams) {
	var tagSelected = {},
		projectId = $routeParams.projectId,
		forEach = angular.forEach,
		tags = $scope.tagOption = {};

	$scope.$watchCollection('tagOption', function () {
		var newTS = {};
		forEach(tags, function (v, t) {
			if (v) {
				newTS[t] = true;
			}
		});
		tagSelected = newTS;
	});
	$scope.projectId = projectId;
	$scope.tagFilter = function (c) {
		var flag = true;
		forEach(tagSelected, function (v, tag) {
			flag = flag && c.tags.indexOf(tag) !== -1;
		});

		return flag;
	};

	// init
	$scope.queryConfig.clear().setActive(true);
	$scope.navConfig.setHref('#/project/' + projectId).setType('back');
	$scope.caseList = Case.query({
		mask: 'comment,code',
		projectId: projectId
	}, function () {
		forEach($scope.caseList, function (c) {
			forEach(c.tags, function (t) {
				tags[t] = false;
			});
		});
	});
});
